define(["jquery","bbcare"],function($,$bb){
    var servers = {};
    var URL = $bb.baseUrl("followup");
    /*
    * 获取问卷：
    * */
    servers.getQuestions = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getAllPostion",
            data:JSON.stringify({
                data :{
                    "examinationPaperName":params.type,
                    "userId":params.userId
                }
            }),
            success: function(data){
                callback(data,otherParams);
            }
        });
    };
    /*
    * 提交问卷：
    * */
    servers.saveQuestions = function(params,callback,otherParams){
        $bb.ajax({
            url: URL + "followup/service/patient/evaluation/getScore",
            data:JSON.stringify({
                data :{
                    "examinationPaperName": params.examinationPaperName,//OSI
                    "postionIds": params.postionIds,
                    "userId": params.userId,
                    "postionScores": params.postionScores
                }
            }),
            success: function(data){
                callback(data,otherParams);
            }
        });
    };
    /*
    * 处理问卷：
    * */
    var topicType = function(des1,list){
        var des3 = [];
        for(var j = 0;j<des1.length;j++){
            var text2;
            if(des1[j].indexOf("text") == 0){
                text2 = "text";
                if(des1[j].indexOf("-") >0){
                    var leiXing2 = des1[j].substring(des1[j].indexOf("-")+1);
                    if(leiXing2 == "boxes"){
                        list.type = "blankList";//需要填入多条；
                    }else if(leiXing2 == "num"){
                        text2 = "number";
                    }else if(leiXing2 == "time"){
                        text2 = "time";
                    }else if(leiXing2 == "date"){
                        text2 = "date";
                    }
                }
            }else{
                text2 = des1[j];
            }
            des3.push({"item":text2});
        }
        return des3;
    };
    servers.getTotalQuestions_osi = function(questions,type){
        for(var i = 0; i< questions.length;i++){
            for(var k = 0;k<questions[i].questions.length;k++){
                var bak = [];
                if(questions[i].questions[k].bak){
                    bak.push({"text":questions[i].questions[k].bak});
                }
                questions[i].questions[k].itemTitle = questions[i].title;//获取每个group的问卷长度：
                if(questions[i].questions[k].list){//联动类题型获取id；
                    var childId = questions[i].questions[k].list[0].id;
                    /*if(childId && childId.indexOf("-") >= 0){
                        questions[i].questions[k].id = childId.substring(0,childId.indexOf("-"));
                    }else{*/
                        questions[i].questions[k].id = childId;
                   /* }*/
                    var list = questions[i].questions[k].list;
                    var link=[],linkArr = [];//关联的
                    for(var m = 0;m<list.length;m++){
                        var linkId = list[m].id;
                        if($.inArray(linkId,link) >= 0){
                            list[m].css = "none";
                        }
                        if(list[m].option){//选择：
                            if(list[m].choice && list[m].choice != "redio"){
                                list[m].type = "checkbox";
                                if(type == "GLE"){
                                    list[m].description = list[m].id +" "+ list[m].description;
                                }
                            }else{
                                //题目带输入框：
                                if(typeof list[m].description == "object"){
                                    list[m].description.unshift(list[m].id + " ");
                                    if(list[m].id == "2.13.5"){
                                        list[m].description.splice(4,2);
                                    }
                                    list[m].description = topicType(list[m].description,list[m]);
                                    list[m].type = "blankSingle";
                                }else{
                                    if(type == "GLE"){
                                        list[m].description = list[m].id + " " +list[m].description
                                    }
                                    list[m].type = "single";
                                }
                            }
                            if(list[m].choose && typeof list[m].choose == 'object'){
                                linkArr = list[m].link0.concat(list[m].link1);
                                link = link.concat(linkArr);
                            }
                            if(list[m].link){
                                link = link.concat(list[m].link);
                            }
                        }else{
                            list[m].type = "blank";//填空：
                            var des1 = list[m].description;
                            if(list[m].id && list[m].id.indexOf(".")>= 0){
                                des1.unshift(list[m].id +" ");
                            }
                            if(list[m].description){
                                list[m].description = topicType(des1,list[m]);
                            }
                        }
                        list[m].itemTitle = questions[i].title;
                        if(type == "GLE"){
                            var splitId = list[m].id.split(".");
                            list[m].id = splitId.join("-");
                        }
                    }
                    questions[i].questions[k].type = "linkage";//联动类型：
                }else{
                    if(questions[i].questions[k].option){//选择：
                        if(questions[i].questions[k].choice && questions[i].questions[k].choice != "redio"){
                            questions[i].questions[k].type = "checkbox";
                        }else{
                            questions[i].questions[k].type = "single";
                        }
                        if(type == "GLE"){
                            questions[i].questions[k].description = questions[i].questions[k].id + " " +questions[i].questions[k].description;
                        }
                    }else{
                        questions[i].questions[k].type = "blank";//需要填入多条；
                        var des = questions[i].questions[k].description;
                        if(questions[i].questions[k].id && questions[i].questions[k].id.indexOf(".")>= 0){
                            des.unshift(questions[i].questions[k].id +" ");
                        }
                        questions[i].questions[k].description = topicType(des,questions[i].questions[k]);
                    }
                }
                if(questions[i].bak){
                    bak.push({"text":questions[i].bak});
                }
                if(bak.length > 0){
                    questions[i].questions[k].bak = JSON.stringify(bak) ;
                }
                if(questions[i].questions[k].id.indexOf(".")>= 0){
                    var splitId = questions[i].questions[k].id.split(".");
                    questions[i].questions[k].id = splitId.join("-");
                }
            }
        }
    }
    servers.getQuestions_osi = function(totalQuestions){
        var questions = {};
        for(var i = 0; i< totalQuestions.length;i++){
            totalQuestions[i].listNo = i+1;
            questions[i+1] = totalQuestions[i];
        }
        return questions
    };

    /*
     * 问卷答案处理：
     * */
    servers.lesAnswerDeal = function(question,nextId,scoreObj){
        for(var i = 0;i<question[nextId].questions.length;i++){
            var fuId = question[nextId].questions[i].fuId;
            var childId = question[nextId].questions[i].id;
            if(childId != 5){
                if(scoreObj["ques"][fuId+"."+childId]){
                    question[nextId].questions[i].value = question[nextId].questions[i].option[scoreObj["ques"][fuId+"."+childId]];
                }
            }else{
                if(childId == 5){
                    if(question[nextId].questions[i].name=="5.请填写该事件发生的年月:"){
                        var times = [];
                        if(scoreObj["ques"][fuId+"."+childId]){
                            times.push(scoreObj["ques"][fuId+"."+childId]);
                            if(scoreObj["manyTimes"][fuId+"."+childId]){
                                for(var item in scoreObj["manyTimes"][fuId+"."+childId]){
                                    times.push(scoreObj["manyTimes"][fuId+"."+childId][item].value);
                                }
                            }
                            question[nextId].questions[i].value = times.join(";");
                        }
                    }else{
                        if(scoreObj["ques"][fuId+"."+childId]){
                            var times2 = JSON.parse(scoreObj["ques"][fuId+"."+childId])//{"startTime":"2017-12-03","endTime":"至今"}
                            question[nextId].questions[i].value = "开始时间："+times2.startTime+";结束时间："+times2.endTime;
                        }
                    }
                }
            }
        }
    };
    return servers;
});